import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/base/BaseState.dart';
import 'package:play_android/com/zhihaoliang/const/AppApi.dart';
import 'package:play_android/com/zhihaoliang/http/HttpException.dart';
import 'package:play_android/com/zhihaoliang/play/article/view/ArticleDetailPage.dart';
import 'package:play_android/com/zhihaoliang/play/common/article/ArticleItem.dart';
import 'package:play_android/com/zhihaoliang/play/home/model/banner/BannerItem.dart';
import 'package:play_android/com/zhihaoliang/play/presenter/ArticlePresenter.dart';
import 'package:play_android/com/zhihaoliang/play/presenter/BannerPresenter.dart';
import 'package:play_android/com/zhihaoliang/widget/EndLine.dart';
import 'package:play_android/com/zhihaoliang/widget/NoData.dart';
import 'package:play_android/com/zhihaoliang/widget/SlideView.dart';

import 'ArticleItemPage.dart';

///  ***************************************************************************
///  左面"主页"的页面
///  ***************************************************************************

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage>
    with BaseState
    implements BannerCallBack, SlideCallBack, ArticleCallBack {
  ///文章的列表数据
  List<ArticleItem> _listData;

  ///轮训页面
  SlideView<BannerItem> _bannerView;

  ///获取首页滚动的数据
  BannerPresenter _bannerPresenter;

  ///获取首页列表数据
  ArticlePresenter _articlePresenter;

  //滑动控件
  ScrollController _contraller = ScrollController();

  _HomePageState() {
    _contraller.addListener(() {
      if (_articlePresenter == null) {
        return;
      }

      if (_articlePresenter.isEnd()) {
        return;
      }

      var maxScroll = _contraller.position.maxScrollExtent;
      var pixels = _contraller.position.pixels;

      if (maxScroll == pixels) {
        _articlePresenter.load();
      }
    });
  }

  @override
  void initState() {
    super.initState();
    _bannerPresenter = BannerPresenter(this);
    _bannerPresenter.load();

    _articlePresenter = new ArticlePresenter(this,AppApi.ARTICLE_LIST);
  }

  @override
  void dispose() {
    _contraller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (_listData == null) {
      return Center(
        child: new CircularProgressIndicator(),
      );
    }

    if (_listData.length == 0) {
      return Center(
        child: new NoData(),
      );
    }

   ListView listView =  ListView.builder(
      itemCount: _listData.length + 1,
      itemBuilder: (context, postion) => _buildItem(postion),
      controller: _contraller,
    );

    return RefreshIndicator(child: listView, onRefresh:_pullToRefresh);
  }

  Future<Null> _pullToRefresh() async {
    _bannerPresenter.load();
  }

  Widget _buildItem(int postion) {
    if (postion == 0) {
      return new Container(
        height: 180.0,
        child: _bannerView,
      );
    }

    postion -= 1;

    ArticleItem homeItem = _listData[postion];
    if (homeItem.type == ArticleItem.END) {
      return EndLine();
    }

    return ArticleItemPage(homeItem);
  }

  @override
  void onLoadBannerError(HttpException httpException) {
    onTipError(httpException, context, "获取轮训图片失败");
  }

  @override
  void onLoadBannerComplete(List<BannerItem> list) {
    _articlePresenter.loadFrist();
    setState(() {
      if (list == null) {
        list = [];
      }

      _bannerView = SlideView<BannerItem>(list, this);
    });
  }

  @override
  handOnItemClick(SlideImp item) {
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new ArticleDetailPage(
          title: item.slideTitle(), url: item.slideUrl());
    }));
  }

  @override
  void onLoadArticleComplete(List<ArticleItem> homeData) {
    setState(() {
      _listData = homeData;
    });
  }

  @override
  void onLoadArticleError(HttpException httpException) {
    if (!_articlePresenter.isFrist()) {
      return;
    }
    onTipError(httpException, context, "获取主页数据失败");
  }
}
